package com.jy.wms.controller;

import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.jy.wms.pojo.WmsQualityTesting;
import com.jy.wms.service.WmsQualityTestingService;
import javax.servlet.http.HttpSession;
import com.github.pagehelper.Page;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jy.wms.common.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @ClassName: WmsQualityTestingController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author LH
 * @Date Fri Nov 03 11:02:51 GMT+08:00 2017
 */
@RestController
@RequestMapping("/wmsqualitytesting")
@Api(value = "/wmsqualitytesting", description = "关于质检规则操作")
public class  WmsQualityTestingController extends BaseController<WmsQualityTestingService,WmsQualityTesting> {

    @PostMapping(value="/list")
    @ApiOperation(value = "查询信息",response = JSONObject.class, notes = "查询信息")
    public @ResponseBody ResultPage<WmsQualityTesting> getWmsQualityTesting(@RequestBody WmsQualityTesting entity,HttpSession session) {
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);//用户信息
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主
        String hzid=null;
        String hzmc=null;
        if (hz.size() > 0) {
            hzid = hz.get(0).getHzid();
            hzmc=hz.get(0).getShow_hzid();
        }
        Page<WmsQualityTesting> list= baseService.baseSelectPage(entity,hzid,hzmc);
        return renderPage(list);
    }

  /*  @PostMapping(value = "delete")
    @ApiOperation(value = "删除信息",response = JSONObject.class, notes = "删除信息")
    public @ResponseBody JsonResult deleteWmsQualityTesting(@RequestBody List<String> ids) {
        Integer flag=baseService.baseDeleteBathPrimaryKeys(ids);
        return renderSuccess("删除成功");
    }*/

    @PostMapping(value = "add")
    @ApiOperation(value = "新增信息",response = JSONObject.class, notes = "新增信息")
    public @ResponseBody JSONObject addWmsQualityTesting(@RequestBody WmsQualityTesting entity,HttpSession session) {
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);//用户信息
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        String ckmc = userinfo.getUser().getOrgName();//仓库名称
        String hzid = null;
        String gysid = null;
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主
        entity.setId(UUIDUtils.uuid());
        entity.setCjr(userinfo.getUser().getName());
        entity.setCkid(ckid);
        if (hz.size() > 0) {
            hzid = hz.get(0).getHzid();
            entity.setSshzid(hzid);
        }
        try {
            baseService.baseInsert(entity);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "质检规则增加成功", "", JSONObject.fromObject(entity).toString());
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "质检规则增加成功", "", JSONObject.fromObject(entity).toString());
        } catch (ServiceException e) {
            baseService.baseDeleteByPrimaryKey(entity.getId());
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "质检规则增加失败", e.getMessage(), "");
        }catch (Exception e){
            baseService.deleteById(entity.getId());
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "质检规则增加失败", e.getMessage(), "");
        }

    }

  /*  @GetMapping(value = "getWmsQualityTesting/{id}")
    @ApiOperation(value = "查看单个信息",response = JSONObject.class, notes = "GET查看单个信息")
    public @ResponseBody WmsQualityTesting getWmsQualityTestingById(@PathVariable("id") String id) {
        WmsQualityTesting entity=baseService.baseSelectByPrimaryKey(id);
        return entity;
    }*/
    @PostMapping(value = "edit")
    @ApiOperation(value = "编辑信息",response = JSONObject.class, notes = "编辑信息")
    public @ResponseBody JSONObject editWmsQualityTesting(@RequestBody WmsQualityTesting entity,HttpSession session) {
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);//用户信息
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主
       String hzid=null;
        if (hz.size() > 0) {
            hzid = hz.get(0).getHzid();
        }
        try {
            Integer flag=baseService.baseUpdateByPrimaryKeySelective(entity,hzid);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "质检规则修改成功", "", JSONObject.fromObject(entity).toString());
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "质检规则修改失败", e.getMessage(), "");
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "质检规则修改成功", "", JSONObject.fromObject(entity).toString());
        }catch (Exception e){
            baseService.deleteById(entity.getId());
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "质检规则修改失败", e.getMessage(), "");
        }

    }

    @PostMapping(value="/selectZjgzBymc")
    @ApiOperation(value = "质检规则",response = JSONObject.class, notes = "质检规则明细")
    public @ResponseBody List<WmsQualityTesting> selectZjgzBymc(@RequestBody Map<String,Object> map, HttpSession session) {
        List<WmsQualityTesting> list=new ArrayList<>();
        try {
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);//用户信息
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
        String sshzid=null;
        if(hz.size()>0){
            sshzid=hz.get(0).getHzid();
        }
        String mc="";
        if(map.containsKey("mc")){mc=map.get("mc").toString();}
         list= baseService.selectZjgzBymc(mc,ckid,sshzid);

        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }
}
